define(['jquery', 'bootstrap', 'backend', 'table', 'form','upload'], function ($, undefined, Backend, Table, Form,Upload) {

    var Controller = {
        index: function () {
            Form.api.bindevent($("form[role=form]")); // 加载富文本编辑器
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/membership_application/index',
                    add_url: 'user/membership_application/add',
                    edit_url: 'user/membership_application/edit',
                    table: 'membership_application',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                search: false,//是否启用搜索框。
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true,operate:false,},
                        // {field: 'user_id', title: '用户id'},
                        {
                            field: 'user_nickname',
                            title: '用户名',
                            operate:'LIKE',
                            formatter: function(value, row, index) {
                                return row.user.nickname; // 使用user.nickname的值作为user_nickname字段的值
                            }
                        },
                        {
                            field: 'goods_type_name',
                            title: '申请类型',
                            searchList: {
                                'categories': __('栏目'),
                                'articles': __('文章'),
                                'user_group': __('会员分组'),
                                'album': __('专辑'),
                            }
                        },
                        {field: 'goods_name', title: '商品名称',operate:false},
                        {field: 'price', title: '商品价格(元)'},
                        {field: 'createtime', title: '申请时间',operate: false,addclass: 'datetimerange',formatter: Table.api.formatter.datetime},
                        {
                            field: 'pay_status',
                            title: '是否通过',
                            formatter: Table.api.formatter.status,
                            searchList: {
                                1: __('是'),
                                0: __('否')
                            }
                        },

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons:[
                                // {
                                //     name: 'detail',
                                //     text: __('Integral Flow'),
                                //     title: __('Integral Flow'),
                                //     classname: 'btn btn-xs btn-primary btn-dialog',
                                //     icon: 'fa fa-list',
                                //     url: '/nYFpaxNKLR.php/score/index?ids={ids}',
                                //     callback: function (data) {
                                //         Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                //     },
                                //     visible: function (row) {
                                //         //返回true时按钮显示,返回false隐藏
                                //         return true;
                                //     }
                                // }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        uploadServiceAgreement: function () {
            Controller.api.bindevent();
        }
    };

    //提交表单
    // Upload.api.plupload($("#faupload-avatar"), function(data, ret){
    //     Fast.api.ajax({
    //         url: '/nYFpaxNKLR.php/user/user/uploadServiceAgreement',
    //         data: {url: data.url}
    //     })
    // }, function(data, ret){
    //     Layer.alert(ret.msg);
    // });

    //富文本编辑器
    Form.api.bindevent($("form[role=form]"));

    //打开服务协议页面
    $(document).on('click', '#btn-import-file', function () {
            Layer.open({
                title: __('Service Agreement'),
                content: Fast.api.fixurl('/nYFpaxNKLR.php/user/user/uploadServiceAgreement'),
                type:2,
                area: ['80%','80%'],
                btn: ['保存'],
                yes: function (index, layero) {
                    //获取表达那元素
                    var iframeDoc = layero.find('iframe')[0].contentDocument;
                    var service_agreement = $(iframeDoc).find("#c-avatar").val();
                    var service_agreement_text = $(iframeDoc).find("#service_agreement_text").val();

                    $.ajax({
                        url: '/nYFpaxNKLR.php/user/user/uploadServiceAgreement',
                        method: 'POST',
                        data: {
                            'service_agreement':service_agreement,
                            'service_agreement_text':service_agreement_text,
                        }, // 序列化表单数据
                        success: function(response) {
                            // 处理服务器返回的响应
                            console.log(response);
                            Layer.close(index);//关闭窗口
                        },
                    });
                }
            });
    });

    return Controller;
});
